package org.tio.sitexxx.service.service.base;

import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.model.main.IpInfo;
import org.tio.utils.cache.CacheUtils;
import org.tio.utils.cache.FirsthandCreater;
import org.tio.utils.cache.ICache;

/**
 * @author tanyaowu 
 * 2016年10月25日 下午5:59:49
 */
public class IpInfoService {
	public static final IpInfoService ME = new IpInfoService();

	/**
	 * 
	 * @author tanyaowu
	 */
	public IpInfoService() {
	}

	public IpInfo getById(Integer id) {
		if (id == null) {
			return null;
		}
		ICache cache = Caches.getCache(CacheConfig.ID_IPINFO);
		String key = id + "";

		IpInfo ipInfo1 = CacheUtils.get(cache, key, false, new FirsthandCreater<IpInfo>() {
			@Override
			public IpInfo create() {
				return IpInfo.dao.findFirst("select * from ip_info where id=?", id);
			}
		});

		return ipInfo1;
	}

	/**
	 * 会先检查缓存和数据库中是不是已经有了各字段值都一样的ip，如果有，则用原来的记录，省得插入冗余的记录
	 * @param ipInfo
	 * @return
	 * @author tanyaowu
	 */
	public IpInfo save(IpInfo ipInfo) {
		ICache cache = Caches.getCache(CacheConfig.IP_IPINFO);

		String key = ipInfo.getIp();

		IpInfo ipInfo1 = CacheUtils.get(cache, key, false, new FirsthandCreater<IpInfo>() {
			@Override
			public IpInfo create() {
				return IpInfo.dao.findFirst("select * from ip_info where ip=? and country=? and area=? and province=? and city=? and operator=?", key, ipInfo.getCountry(),
				        ipInfo.getArea(), ipInfo.getProvince(), ipInfo.getCity(), ipInfo.getOperator());
			}
		});

		if (ipInfo1 == null || ipInfo1.getId() == null) {
			ipInfo.save();
			cache.put(key, ipInfo);
			return ipInfo;
		} else {
			return ipInfo1;
		}
	}

	/**
	 * 
	 * @param ip
	 * @return
	 * @author tanyaowu
	 */
	public IpInfo save(String ip) {
		IpInfo ipInfo = IpInfo.from(ip);
		return save(ipInfo);
	}

}
